import requests
import os


url = 'https://movie.douban.com/j/search_subjects'
params = {
    "type": "movie",
    "tag": "最新",
    "page_limit": 50,
    "page_start": 0
}
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
}

res = requests.get(url=url,params=params,headers=headers)
print(res.status_code)
print(res.request.headers)
print(res.json())


if not os.path.exists('movies'):
    os.mkdir('movies')


for sub in res.json()["subjects"]:
    # 电影的名字
    name = sub['title']
    # 电影海报的地址
    cover = sub['cover']
    # 发送get请求到地址栏
    r = requests.get(url=cover)
    # 返回的是一张图片
    print(r.status_code,r.content)
    # 将图片信息保存到文件中
    with open(file=f'movies/{name}.png',mode='wb') as f:
        f.write(r.content)